<!--  -->
<template>
  <div class="meedu-main-body">
    <BackBar class="mb-30" title="添加课程分类"></BackBar>
    <div class="float-left">
      <el-form ref="form" :model="user" :rules="rules" label-width="200px">
        <el-form-item label="父级" prop="parent_id">
          <el-select class="w-300px" clearable v-model="user.parent_id">
            <el-option
              v-for="(item, index) in categories"
              :key="index"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="升序" prop="sort">
          <div class="d-flex">
            <div>
              <el-input
                type="number"
                v-model="user.sort"
                class="w-300px"
              ></el-input>
            </div>
            <div class="ml-10">
              <div class="helper-text">
                <i class="el-icon-info"></i>
                <span class="ml-5"
                  >请输入整数。小数排在前面，大数排在后面。</span
                >
              </div>
            </div>
          </div>
        </el-form-item>

        <el-form-item label="分类名" prop="name">
          <el-input v-model="user.name" class="w-300px"></el-input>
        </el-form-item>

        <el-form-item label="显示" prop="is_show">
          <el-switch
            v-model="user.is_show"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
    </div>
    <div class="bottom-menus">
      <div class="bottom-menus-box">
        <div>
          <el-button @click="formValidate" :loading="loading" type="primary">
            保存
          </el-button>
        </div>
        <div class="ml-24">
          <el-button @click="$router.back()">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import BackBar from "@/components/back-bar";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { BackBar },
  data() {
    return {
      user: {
        sort: null,
        name: null,
        is_show: 1,
        parent_id: null,
      },
      rules: {
        sort: [
          {
            required: true,
            message: "升序不能为空",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "分类名不能为空",
            trigger: "blur",
          },
        ],
      },
      categories: [],
      loading: false,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    params() {
      this.$request.get(`courseCategory/create`).then((res) => {
        this.categories = res.data.categories;
      });
    },
    formValidate() {
      this.$refs["form"].validate((valid) => {
        if (valid) this.confirm();
      });
    },
    confirm() {
      if (this.loading) return;
      this.loading = true;
      this.$request
        .post(`courseCategory`, this.user)
        .then(() => {
          this.$message.success(this.$t("common.success"));
          this.$router.back();
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.params();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
@import url("@/assets/common/variable.less");
</style>